﻿using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;

namespace AnyOffice.general.email
{
    public partial class box : ModulePage
    {
        public string FormatSize(int boxId)
        {
            return StringHelper.FormatSize(new EmailBoxDao().SpecifyBoxSize(base.Conn, base.User.Identity.Name, boxId));
        }
        private void BindData()
        {
            DataTable table = new EmailBoxDao().SelectAll(base.Conn, base.User.Identity.Name);
            if (table.Rows.Count > 0)
            {
                this.rptBox.DataSource = table;
                this.rptBox.DataBind();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                EmailBoxDao dao = new EmailBoxDao();
                this.litInbox.Text = StringHelper.FormatSize(dao.SpecifyBoxSize(base.Conn, base.User.Identity.Name, 0));
                this.litOutbox.Text = StringHelper.FormatSize(dao.OutboxSize(base.Conn, base.User.Identity.Name));
                int emailCapacity = new UserCache().EmailCapacity;
                if (emailCapacity == 0)
                {
                    this.litEmailCapacity.Text = "不限制";
                }
                else
                {
                    this.litEmailCapacity.Text = StringHelper.FormatSize((long)((emailCapacity * 1024) * 1024));
                }
                this.litTotal.Text = StringHelper.FormatSize(dao.InboxSize(base.Conn, base.User.Identity.Name));
                this.BindData();
            }
            if (this.Page.IsPostBack)
            {
                EmailBoxDao dao2 = new EmailBoxDao();
                EmailBoxData data = new EmailBoxData();
                data.UserId = base.User.Identity.Name;
                data.BoxNo = Convert.ToInt32(this.txtBoxNo.Text);
                data.BoxName = this.txtBoxName.Text;
                dao2.Insert(base.Conn, data);
                this.txtBoxNo.Text = string.Empty;
                this.txtBoxName.Text = string.Empty;
                this.BindData();
                if (!this.ClientScript.IsStartupScriptRegistered(this.GetType(), "refresh"))
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "refresh", "window.parent.mail_menu.location.reload();", true);
                }
            }
        } 

    }
}
